home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15535 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: HOPPER.ACM.ORG!news
  2. From: varnk@e62.diebold.com (Ken Varn)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is wrong with this loop?
  5. Date: 19 Apr 1996 18:26:46 GMT
  6. Organization: Diebold
  7. Distribution: world
  8. Message-ID: <4l8lt7$d9h@HOPPER.ACM.ORG>
  9. References: <4l86la$1t9@uwm.edu>
  10. NNTP-Posting-Host: 199.218.232.47
  11. Mime-Version: 1.0
  12. Content-Type: Text/Plain; charset=US-ASCII
  13. X-Newsreader: WinVN 0.99.7
  14.  
  15. In article <4l86la$1t9@uwm.edu>, mardavuy@alpha2.csd.uwm.edu says...
  16. />
  17. />#include <stdio.h>
  18. />int main(void)
  19. />{
  20. />   int dia;
  21. />
  22. />   char cd;
  23. />
  24. />   ...
  25. />
  26. />   scanf("%c", &cd);
  27. />   while (cd != 'm' || cd ! 'f' || cd != 'o')
  28. />        {
  29. />        printf("Re-enter m, f, or o.\n");
  30. />        scanf(%c", &cd);
  31. />        }
  32. />   ...
  33. />}
  34. />
  35. />It works fine, except that it gives me the printf() twice.
  36.  
  37.  
  38. Try changing your scanf() as follows:
  39.  
  40. scanf("%c ",&cd);
  41.  
  42. This tells scanf() to read past any white space characters after the 
  43. charadter read.
  44.  
  45. The reason you were seeing the problem that you had is because when you
  46. press enter after typing the character, the \n is appended to the stdin 
  47. buffer, but you have not read it out.  The next call to scanf() tried to read 
  48. a character out of the stdin buffer, but only saw the \n, so it returns 
  49. immediately.
  50.  
  51. The scanf() function has a lot of tricky idiosyncrasies that need to be 
  52. understood in order to use it properly.  You may want to review your compiler 
  53. documentation on its implementation.
  54.  
  55.